package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.ShopCondition;
import com.spa.infrastructure.repository.mapper.ShopMapper;
import com.spa.infrastructure.repository.po.ShopPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class ShopRepositoryService extends ServiceImpl<ShopMapper, ShopPO> {

    @Resource
    private ShopMapper shopMapper;

    public List<ShopPO> shopList(ShopCondition condition) {
        return shopMapper.selectList(wrapper(condition));
    }

    public Long add(ShopPO po) {
        shopMapper.insert(po);
        return po.getId();
    }

    /**
     * 根据id查询门店
     *
     * @param shopId 门店id
     * @return 门店信息
     */
    public ShopPO getShopById(Long shopId) {
        return shopMapper.selectById(shopId);
    }

    /**
     * 根据指定条件查询门店
     *
     * @param condition 条件
     * @return 门店信息
     */
    public ShopPO getShopByCondition(ShopCondition condition) {
        LambdaQueryWrapper<ShopPO> wrapper = wrapper(condition);
        List<ShopPO> poList = shopMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(poList)) {
            return poList.get(0);
        }
        return null;
    }

    public void updateShopById(ShopPO po) {
        shopMapper.update(null, updateWrapper(po));
    }

    /**
     * 按条件统计门店数量
     *
     * @return 门店数
     */
    public Long countByCondition(ShopCondition condition) {
        return count(wrapper(condition));
    }

    public Page<ShopPO> page(ShopCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<ShopPO> list(ShopCondition condition) {
        return list(wrapper(condition));
    }

    private LambdaQueryWrapper<ShopPO> wrapper(ShopCondition condition) {
        LambdaQueryWrapper<ShopPO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.hasText(condition.getName()), ShopPO::getName, condition.getName());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getId()), ShopPO::getId, condition.getId());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), ShopPO::getId, condition.getIds());
        wrapper.eq(ObjectUtil.isNotNull(condition.getCityId()), ShopPO::getCityId, condition.getCityId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getIsDel()), ShopPO::getIsDel, condition.getIsDel());
        wrapper.orderByDesc(ObjectUtil.isNotNull(condition.getOrderByCreateTimeDesc()), ShopPO::getCreateTime);
        return wrapper;
    }

    private LambdaUpdateWrapper<ShopPO> updateWrapper(ShopPO po) {
        LambdaUpdateWrapper<ShopPO> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ObjectUtil.isNotNull(po.getId()), ShopPO::getId, po.getId());
        wrapper.set(StringUtils.hasText(po.getPhone()), ShopPO::getPhone, po.getPhone());
        wrapper.set(StringUtils.hasText(po.getAddress()), ShopPO::getAddress, po.getAddress());
        wrapper.set(StringUtils.hasText(po.getLogo()), ShopPO::getLogo, po.getLogo());
        wrapper.set(StringUtils.hasText(po.getBusinessLicense()), ShopPO::getBusinessLicense, po.getBusinessLicense());
        wrapper.set(StringUtils.hasText(po.getRemark()), ShopPO::getRemark, po.getRemark());
        wrapper.set(StringUtils.hasText(po.getOwner()), ShopPO::getOwner, po.getOwner());
        wrapper.set(ObjectUtil.isNotNull(po.getIsDel()), ShopPO::getIsDel, po.getIsDel());
        wrapper.set(ObjectUtil.isNotNull(po.getFlag()), ShopPO::getFlag, po.getFlag());
        wrapper.set(ObjectUtil.isNotNull(po.getCityId()), ShopPO::getCityId, po.getCityId());
        wrapper.set(StringUtils.hasText(po.getName()), ShopPO::getName, po.getName());
        wrapper.set(ObjectUtils.isNotEmpty(po.getWithdrawPeriod()), ShopPO::getWithdrawPeriod, po.getWithdrawPeriod());
        return wrapper;
    }
}
